<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Category_library
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}
		
		$this->_table = $this->CI->config->item('database_tables');
	}
		
	// Public methods
	public function get_categories($lang_id=null)
	{
		if($lang_id == null)
		{
			$lang_id = 2; //default language is vietnamese
		}
		$this->CI->db->select('id, name, url_name,status,title');
		$this->CI->db->from($this->_table['category'] . ' category');
		$this->CI->db->where('lang_id',$lang_id);
		$this->CI->db->where('status','1');
		$this->CI->db->select('(SELECT COUNT(' . $this->CI->db->dbprefix . 'newscategory.id) FROM ' . $this->CI->db->dbprefix . 'newscategory WHERE ' . $this->CI->db->dbprefix . 'newscategory.category_id = category.id) AS posts_count', FALSE); 
		$this->CI->db->order_by('id', 'ASC');
		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			return $query->result_array();
		}
	}
	
	public function get_categories_by($lang_id=null,$parent_id)
	{
		if($lang_id == null)
		{
			$lang_id = 2; //default language is vietnamese
		}
		$this->CI->db->select('id, name, url_name,status,title');
		$this->CI->db->from($this->_table['category'] . ' category');
		$this->CI->db->where('lang_id',$lang_id);
		$this->CI->db->where('status','1');
		$this->CI->db->where('parent_id',$parent_id);
		$this->CI->db->select('(SELECT COUNT(' . $this->CI->db->dbprefix . 'newscategory.id) FROM ' . $this->CI->db->dbprefix . 'newscategory WHERE ' . $this->CI->db->dbprefix . 'newscategory.category_id = category.id) AS posts_count', FALSE); 
		$this->CI->db->order_by('id', 'ASC');
		
		$query = $this->CI->db->get();
			
		if ($query->num_rows() > 0)
		{			
			return $query->result_array();
		}
		
	}
}

/* End of file Categories_library.php */
/* Location: ./application/libraries/Categories_library.php */